<?php

namespace App\Transformers\Admin;

use League\Fractal\TransformerAbstract;

class RoleTransformer extends TransformerAbstract
{
    //获取权限额外数据的关键字
    protected $availableIncludes = ['permissions'];

    /**
     * 角色的转换层
     * @param $role
     * @return array
     */
    public function transform($role)
    {
        $permission_id = $role->permissions()->pluck('permission_id')->toArray();

        return [
            'id' => $role->id,
            'name' => $role->name,
            'cn_name' => $role->cn_name,
            'permission_id' => $permission_id
        ];
    }


    /**
     * 获取额外的权限数据
     * @param $role
     * @return \League\Fractal\Resource\Collection
     */
    public function includePermissions($role)
    {
        $permissions = $role->permissions()->where('pid',0)->get();
        return $this->collection($permissions, new PermissionTransformer());
    }
}

